import time
import pygame
from pygame.locals import *

# 定义英雄飞机的横坐标
hero_x = 150

# 定义英雄飞机的纵坐标
hero_y = 600

# 定义一个弹仓列表
my_bullet = []

enemy_x = 120

enemy_path = 'right'

enemy_num = 0

enemy_blood_volume = 1000

# 加载飞机爆炸的多张图片
a = pygame.image.load('../feiji/enemy2_down1.png')
b = pygame.image.load('../feiji/enemy2_down2.png')
c = pygame.image.load('../feiji/enemy2_down3.png')
d = pygame.image.load('../feiji/enemy2_down4.png')
e = pygame.image.load('../feiji/enemy2_down5.png')
f = pygame.image.load('../feiji/enemy2_down6.png')

# 定义一个列表（爆炸）
blow_up = [a,b,c,d,e,f]
enemy_life = 'live' # live 存活 dead 死亡

# 定义一个函数hero_plane
def hero_plane(screen,hero,bullet):
    global hero_x
    global hero_y
    # 粘贴英雄飞机图片
    screen.blit(hero,(hero_x,hero_y))

    # 获取pygame键盘按下事件，pygame.event.get()后去所有事件，返回结果是一个列表
    # print(pygame.event.get())
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        elif event.type == KEYDOWN:
            if (hero_x > 0 and hero_x < 300):
                if event.key == K_UP:
                    hero_y -= 10
                    print('您按了向上键')
                    pass
                elif event.key == K_DOWN:
                    hero_y += 10
                    print('您按了向下键')
                    pass
                elif event.key == K_LEFT:
                    hero_x -= 10
                    print('您按了向左键')
                    pass
                elif event.key == K_RIGHT:
                    hero_x += 10
                    print('您按了向右键')
                    pass
                elif event.key == K_SPACE:
                    print('发生子弹')
                    my_bullet.append({'x': hero_x + 32, 'y': hero_y - 40})
            else:
                if hero_x < 0:
                    hero_x = 1
                elif hero_x > 300:
                    hero_x = 299

    # 编写循环，遍历my_bullet生成子弹图片，然后让子弹可以弹出
    for i in my_bullet:
        screen.blit(bullet,(i['x'],i['y']))
        # screen.blit(bullet,(i['x'] + 65,i['y']))
        i['y'] -= 20


def enemy_plane(screen,enemy):
    global enemy_x
    global enemy_path
    global enemy_life
    global enemy_num
    global enemy_blood_volume
    if enemy_life == 'live':
        # 粘贴敌机
        screen.blit(enemy, (enemy_x, 0))
        if enemy_x >= 235:
            enemy_path = 'left'
        elif enemy_x <= 0:
            enemy_path = 'right'

        for bullet in my_bullet:
            if (bullet['x'] >= enemy_x and bullet['x'] <= enemy_x + 160) and (bullet['y'] >= 0 and bullet['y'] <= 240):
                print(enemy_blood_volume)
                enemy_blood_volume -= 10

            if enemy_blood_volume <= 0:
                enemy_life = 'dead'

        if enemy_path == 'left':
            enemy_x -= 10
        elif enemy_path == 'right':
            enemy_x += 10

    elif enemy_life == 'dead':
        if enemy_num <= 5:
            screen.blit(blow_up[enemy_num],(enemy_x,0))
            enemy_num += 1



# 定义一个主函数
def main():
    # 创建一个Window窗口对象
    screen = pygame.display.set_mode((400,800),0,32)
    # 创建一个background变量，用于存放飞机大战背景图片
    background = pygame.image.load("../feiji/background.png")
    # 创建一个变量，用于存放飞机大战背景图片信息
    hero = pygame.image.load('../feiji/hero1.png')
    # 加在子弹图片
    bullet = pygame.image.load('../feiji/plane.png')
    # 加载敌机
    enemy = pygame.image.load('../feiji/enemy2_n2.png')
    # 在Windows窗口中，有一个专门的方法blit()，用于对图片进行粘贴操作（粘贴到窗口中）
    # 窗口对象blit.(要粘贴的图片对象，(横坐标，纵坐标))
    while True:
        screen.blit(background,(0,0))
        # 创建一个函数hero_plane，用于粘贴英雄飞机以及移动等操作
        hero_plane(screen,hero,bullet)
        # 创建一个enemy_plane函数，用于粘贴敌机
        enemy_plane(screen,enemy)
        # 为pygame窗口添加一个标题
        pygame.display.set_caption("飞机大战")
        # 刷新Windows窗口，让图片立即生效
        pygame.display.update()
        # 休眠
        time.sleep(0.1)

# 定义程序的入口
if  __name__== "__main__":
    main()